package servletmvc.imooc.util;

import java.sql.DriverManager;

import com.mysql.jdbc.Connection;

public class DBUtil {
	private static String Driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/servlet?useUnicode=true&characterEncoding=utf8";
	private static String username = "WnplV/ietfQ=";
	private static String password = "WnplV/ietfQ=";
	private static Connection conn = null;
	
	//连接数据库
	public static Connection getConnection() {
		if(conn == null) {
			try {
				Class.forName(Driver);//加载驱动器
				conn = (Connection) DriverManager.getConnection(url,
						DESUtil.getDecryptString(username), 
						DESUtil.getDecryptString(password));
				System.out.println("开始数据库连接");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
	
	//关闭数据库联接
	public static void CloseConn() {
		if(conn != null) {
			try {
				conn.close();
				System.out.println("关闭数据库连接");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	} 
	
	public static void main(String[] args) {
		getConnection();
		CloseConn();
	}
}
